//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package gmapimagecutter;

import imagetoolscommon.ImageData;
import imagetoolscommon.ImageTileCutterThread;
import imagetoolscommon.Utils;

import javax.imageio.IIOImage;
import javax.imageio.metadata.IIOMetadata;
import javax.imageio.stream.FileImageOutputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.io.*;
import java.net.URL;
import java.util.List;

public class GMICCutterThread3 extends ImageTileCutterThread implements Cloneable {
    private boolean highQuality = true;

    public GMICCutterThread3() {
    }

    public Object clone() throws CloneNotSupportedException {
        GMICCutterThread3 thread = (GMICCutterThread3)super.clone();
        thread.highQuality = this.highQuality;
        return thread;
    }

    public boolean isHighQuality() {
        return this.highQuality;
    }

    public void setHighQuality(boolean highQuality) {
        this.highQuality = highQuality;
    }

    public static void generateHTML(String templateHTML, String mainHTMLName, String createDir, String tileDir, int maxZoom) throws Exception {
        String apiKeyTag = "<%APIKEY>";
        String apiVersionTag = "<%APIVERSION>";
        String mapTitleTag = "<%MAPTITLE>";
        String maxZoomTag = "<%MAXZOOM>";
        String maxZoomP1Tag = "<%MAXZOOMP1>";
        String initialZoomTag = "<%INITIALZOOM>";
        String centreLatTag = "<%CENTRELAT>";
        String centreLonTag = "<%CENTRELON>";
        String tileDirTag = "<%TILEDIR>";
        double centreLat = 0.0D;
        double centreLon = 0.0D;
        int initialZoom = maxZoom >> 1;
        URL templateURL = GMapImgCutApp.class.getResource(templateHTML);
        BufferedReader reader = new BufferedReader(new InputStreamReader(templateURL.openStream()));
        BufferedWriter writer = new BufferedWriter(new FileWriter(createDir + "/" + mainHTMLName));

        String line;
        while((line = reader.readLine()) != null) {
            if(line.indexOf("<%") >= 0) {
                line = line.replaceAll("<%APIKEY>", "PUTAPIKEYHERE");
                line = line.replaceAll("<%APIVERSION>", "2.x");
                line = line.replaceAll("<%MAPTITLE>", "Picture Viewer");
                line = line.replaceAll("<%MAXZOOM>", Integer.toString(maxZoom));
                line = line.replaceAll("<%MAXZOOMP1>", Integer.toString(maxZoom + 1));
                line = line.replaceAll("<%INITIALZOOM>", Integer.toString(initialZoom));
                line = line.replaceAll("<%CENTRELAT>", Double.toString(centreLat));
                line = line.replaceAll("<%CENTRELON>", Double.toString(centreLon));
                line = line.replaceAll("<%TILEDIR>", tileDir);
            }

            writer.write(line);
            writer.newLine();
        }

        writer.close();
        reader.close();
    }

    private void makeTiles(int depth, double x, double y, double width, double height, String tileName) {
        if(!this.abortCreate) {
            if(depth > 0) {
                BufferedImage tileImg = new BufferedImage(256, 256, 1);
                Graphics2D g = tileImg.createGraphics();
                if(this.highQuality) {
                    BufferedImage centreX = Utils.get256pxScaledInstance(this.originalImage, (int)x, (int)y, (int)(x + width), (int)(y + height));
                    g.drawImage(centreX, 0, 0, (ImageObserver)null);
                } else {
                    g.drawImage(this.originalImage, 0, 0, 256, 256, (int)x, (int)y, (int)(x + width), (int)(y + height), Color.black, (ImageObserver)null);
                }

                g.dispose();

                try {
                    ++this.currentTileCount;
                    if(this.tileObserver != null) {
                        this.tileObserver.processingTile(tileName + ".jpg", this.currentTileCount, this.totalTileCount);
                    }

                    this.imageWriter.setOutput(new FileImageOutputStream(new File(this.strTileDir + tileName + ".jpg")));
                    IIOImage var18 = new IIOImage(tileImg, (List)null, (IIOMetadata)null);
                    this.imageWriter.write((IIOMetadata)null, var18, this.iwp);
                } catch (IOException var17) {
                    var17.printStackTrace();
                }

                double var19 = x + width / 2.0D;
                double centreY = y + height / 2.0D;
                this.makeTiles(depth - 1, x, y, width / 2.0D, height / 2.0D, tileName + "q");
                this.makeTiles(depth - 1, var19, y, width / 2.0D, height / 2.0D, tileName + "r");
                this.makeTiles(depth - 1, var19, centreY, width / 2.0D, height / 2.0D, tileName + "s");
                this.makeTiles(depth - 1, x, centreY, width / 2.0D, height / 2.0D, tileName + "t");
            }
        }
    }

    public void setParams(ImageData imageData, int depth, boolean highQuality) {
        this.tileObserver = null;
        this.createDepth = depth;
        this.highQuality = highQuality;
        this.totalTileCount = calculateTotalTileCount(depth);
        this.originalImage = imageData.image;
        this.imageFilename = imageData.filename;
        this.strCreateDir = imageData.file.getParent();
        File tileDir = new File(imageData.file.getParentFile(), Utils.changeFileExt(this.imageFilename, "") + "-tiles");
        this.strTileDir = tileDir.getAbsolutePath() + File.separator;
    }

    public void setImageData(ImageData imageData) {
        super.setImageData(imageData);
        this.totalTileCount = calculateTotalTileCount(this.createDepth);
    }

    public void beginCreate(int depth) {
        this.abortCreate = false;
        this.currentTileCount = 0;
        File tileDir = new File(this.strTileDir);
        tileDir.mkdirs();

        try {
            String x = Utils.changeFileExt(this.imageFilename, ".html");
            generateHTML("templatefiles/template.html", x, this.strCreateDir, tileDir.getName(), depth - 1);
        } catch (Exception var9) {
            Utils.showErrorDialogBox((Component)null, "Error Generating HTML Page", (String)null, var9);
            return;
        }

        this.setupImageWriter();
        double x1 = 0.0D;
        double y = 0.0D;
        int width = this.originalImage.getWidth();
        int height = this.originalImage.getHeight();
        if(width < height) {
            x1 = (double)(-(height - width) / 2);
            this.makeTiles(depth, x1, y, (double)height, (double)height, "t");
        } else if(height < width) {
            y = (double)(-(width - height) / 2);
            this.makeTiles(depth, x1, y, (double)width, (double)width, "t");
        } else {
            this.makeTiles(depth, x1, y, (double)width, (double)height, "t");
        }

    }
}
